package com.microservice.wechat.controller;

import com.microservice.api.common.Result;
import com.microservice.api.wechat.WechatServiceClient;
import com.microservice.api.wechat.dto.TokenResponse;
import com.microservice.api.wechat.dto.TokenStatusResponse;
import com.microservice.wechat.service.WechatTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信服务API实现
 * 实现common-api中定义的WechatServiceApi接口
 * 
 * @author wechat-service
 */
@RestController
public class WechatApiController implements WechatServiceClient {
    
    @Autowired
    private WechatTokenService tokenService;
    
    @Override
    public Result<TokenResponse> getToken(String appId) {
        try {
            String token = tokenService.getAccessToken(appId);
            
            TokenResponse response = new TokenResponse();
            response.setAppId(appId);
            response.setToken(token);
            
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("获取Token失败: " + e.getMessage());
        }
    }
    
    @Override
    public Result<String> refreshToken(String appId) {
        try {
            String token = tokenService.forceRefreshToken(appId);
            return Result.success("Token刷新成功", token);
        } catch (Exception e) {
            return Result.error("Token刷新失败: " + e.getMessage());
        }
    }
    
    @Override
    public Result<String> refreshAllTokens() {
        try {
            tokenService.refreshAllTokens();
            return Result.success("批量刷新任务已启动");
        } catch (Exception e) {
            return Result.error("批量刷新失败: " + e.getMessage());
        }
    }
    
    @Override
    public Result<Map<String, TokenStatusResponse>> getTokenStatus() {
        try {
            Map<String, Object> statusMap = tokenService.getAllTokenStatus();
            Map<String, TokenStatusResponse> result = new HashMap<>();
            
            // 转换为TokenStatusResponse
            statusMap.forEach((appId, info) -> {
                @SuppressWarnings("unchecked")
                Map<String, Object> infoMap = (Map<String, Object>) info;
                
                TokenStatusResponse status = new TokenStatusResponse();
                status.setAccountName((String) infoMap.get("accountName"));
                status.setConfigType((String) infoMap.get("configType"));
                status.setHasToken((Boolean) infoMap.get("hasToken"));
                status.setTtl(((Number) infoMap.get("ttl")).longValue());
                status.setTtlMinutes(((Number) infoMap.get("ttlMinutes")).longValue());
                
                result.put(appId, status);
            });
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取状态失败: " + e.getMessage());
        }
    }
    
    @Override
    public Result<TokenResponse> getRandomToken() {
        try {
            Map<String, String> tokenInfo = tokenService.getRandomAccessToken();
            
            TokenResponse response = new TokenResponse();
            response.setAppId(tokenInfo.get("appId"));
            response.setToken(tokenInfo.get("token"));
            response.setAccountName(tokenInfo.get("accountName"));
            response.setConfigType(tokenInfo.get("configType"));
            
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("随机获取Token失败: " + e.getMessage());
        }
    }
    
    @Override
    public Result<TokenResponse> getRandomTokenByType(String type) {
        try {
            Map<String, String> tokenInfo = tokenService.getRandomAccessTokenByType(type);
            
            TokenResponse response = new TokenResponse();
            response.setAppId(tokenInfo.get("appId"));
            response.setToken(tokenInfo.get("token"));
            response.setAccountName(tokenInfo.get("accountName"));
            response.setConfigType(tokenInfo.get("configType"));
            
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("根据类型随机获取Token失败: " + e.getMessage());
        }
    }
}
